package org.fatewa.engine.config;

import org.fatewa.engine.advice.exception.RayquazaException;
import org.fatewa.engine.design.advice.AbstractExceptionProcessor;
import org.fatewa.engine.exception.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;

/**
 * 默认提供的异常处理器，用户也可在 application 模块中自行进行扩展
 * 实现 AbstractExceptionProcessor 提供对应的泛型即可
 *
 * @author 4everlynn
 */

@Configuration
public class BuiltinExceptionProcessor {


    /**
     * @see MethodArgumentTypeMismatchException
     */
    @Bean
    AbstractExceptionProcessor<MethodArgumentTypeMismatchException> methodArgumentTypeMismatchExceptionExceptionProcessor() {
        return new MethodArgumentTypeMismatchProcessor();
    }

    /**
     * @see HttpRequestMethodNotSupportedProcessor
     */
    @Bean
    AbstractExceptionProcessor<HttpRequestMethodNotSupportedException> httpRequestMethodNotSupportedExceptionAbstractExceptionProcessor() {
        return new HttpRequestMethodNotSupportedProcessor();
    }

    /**
     * @see BadSqlGrammarExceptionProcessor
     */
    @Bean
    AbstractExceptionProcessor<BadSqlGrammarException> badSqlGrammarExceptionAbstractExceptionProcessor() {
        return new BadSqlGrammarExceptionProcessor();
    }

    /**
     * @see DuplicateKeyProcessor
     */
    @Bean
    AbstractExceptionProcessor<DuplicateKeyException> duplicateKeyExceptionAbstractExceptionProcessor() {
        return new DuplicateKeyProcessor();
    }


    /**
     * @see RayquazaProcessor
     */
    @Bean
    AbstractExceptionProcessor<RayquazaException> rayquazaExceptionAbstractExceptionProcessor() {
        return new RayquazaProcessor();
    }

    /**
     * @see DataIntegrityViolationProcessor
     */
    @Bean
    AbstractExceptionProcessor<DataIntegrityViolationException> dataIntegrityViolationExceptionAbstractExceptionProcessor() {
        return new DataIntegrityViolationProcessor();
    }

    /**
     * @see DataIntegrityViolationProcessor
     */
    @Bean
    AbstractExceptionProcessor<MethodArgumentNotValidException> methodArgumentNotValidExceptionAbstractExceptionProcessor() {
        return new MethodArgumentNotValidProcessor();
    }

    @Bean
    AbstractExceptionProcessor<HttpMessageNotReadableException> httpMessageNotReadableExceptionAbstractExceptionProcessor() {
        return new HttpMessageNotReadableProcessor();
    }

}
